﻿package Cain 
{
	import flash.events.TimerEvent;
	import flash.net.LocalConnection;
	import flash.utils.Timer;
	import flash.system.System;
	/**
	 * 回收記憶體
	 * @author Cain
	 */
	public class ClearMem
	{
		private var timer:Timer;
		/**
		 * 回收記憶體
		 */
		public function ClearMem()
		{
			timer = new Timer(5000, 0);
			timer.addEventListener(TimerEvent.TIMER,_clear)
		}
		
		private function _clear(e:TimerEvent):void 
		{
			clear();
		}
		/**
		 * 強制回收記憶體
		 */
		public static function clear():void 
		{
			try {  
				new LocalConnection().connect("foo");  
				new LocalConnection().connect("foo");  
			}catch (err:Error){  
     
			}
			System.gc();
			System.gc();
		}
		/**
		 * 定時清理記憶體
		 * @param	_s 豪秒
		 */
		public function startClear(_s:int = 5000):void 
		{
			timer.delay = _s;
			timer.start()
		}
		/**
		 * 定時清理記憶體停止
		 */
		public function stopClear():void 
		{
			timer.stop();
		}
	}

}